package com.interceptor;

import java.io.UnsupportedEncodingException;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.http.HttpServletRequest;

import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.plugin.activerecord.Record;
import com.util.ApplicationUtils;
/**
 * @author 作者 :wangyilong
 * @version 创建时间：2018年4月10日  
 * 类说明:条件查询拦截器
 */
public class InquireInterceptor  implements Interceptor {
	/**
	 * 进行条件拼接
	 * @param model
	 */
	@Override
	public void intercept(Invocation inv) {
		
		HttpServletRequest request= inv.getController().getRequest();
		try {
			request.setCharacterEncoding("utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		Record record=new Record();
		StringBuilder sql  = new StringBuilder();
		Map<String, String[]> parasMap = request.getParameterMap();
		// 对 paraMap进行遍历而不是对table.getColumnTypeMapEntrySet()进行遍历，以便支持 CaseInsensitiveContainerFactory
		// 以及支持界面的 attrName有误时可以感知并抛出异常避免出错
		for (Entry<String, String[]> entry : parasMap.entrySet()) {
			String paraName = entry.getKey();
			String[] paraValueArray = entry.getValue();
			String paraValue = (paraValueArray != null && paraValueArray.length > 0) ? paraValueArray[0] : null;
			if(paraValue!=null&&paraValue!=""){  
				if(paraName.indexOf("typeName")!=-1|paraName.indexOf("city")!=-1|paraName.indexOf("userName")!=-1|paraName.indexOf("nikeName")!=-1
						|paraName.indexOf("name")!=-1|paraName.indexOf("courierName")!=-1|paraName.indexOf("rootResourcetName")!=-1
						|paraName.indexOf("contactPerson")!=-1|paraName.indexOf("pathName")!=-1|paraName.indexOf("path")!=-1 ){
					 
					sql.append(" and "+paraName+" like '%"+paraValue+"%'");
					
				}else if("typeId".equals(paraName)|"dictionaryId".equals(paraName)|paraName.indexOf("phone")!=-1|paraName.indexOf("type")!=-1
|paraName.indexOf("delFlag")!=-1|paraName.indexOf("orderNo")!=-1|paraName.indexOf("outNo")!=-1|paraName.indexOf("purNo")!=-1
|paraName.indexOf("outboundCauseNo")!=-1|paraName.indexOf("warehousingCauseNo")!=-1|paraName.indexOf("userNo")!=-1|paraName.indexOf("commentNo")!=-1
|paraName.indexOf("orderNo")!=-1|paraName.indexOf("courierPhone")!=-1|paraName.indexOf("dictionaryId")!=-1|paraName.indexOf("platform")!=-1
|paraName.indexOf("rootResourcetType")!=-1|paraName.indexOf("hierarchy")!=-1|paraName.indexOf("orderDetailsNo")!=-1|paraName.indexOf("lossOrProfitNo")!=-1
|paraName.indexOf("sysUserNo")!=-1|paraName.indexOf("courierNo")!=-1|paraName.indexOf("expressNumber")!=-1|paraName.indexOf("addressPhone")!=-1
|paraName.indexOf("managementNo")!=-1|paraName.indexOf("YNreturn")!=-1|paraName.indexOf("promotionId")!=-1|paraName.indexOf("dictionaryName")!=-1 ){
					   
					sql.append(" and "+paraName+" = '"+paraValue+"'");
						
				}else if(paraName.indexOf("warehouse")!=-1){//仓库表查询
					
					if("warehouseNo".equals(paraName)|paraName.indexOf("warehouseNo")!=-1){
						if(!paraValue.equals("-1"))sql.append(" and "+paraName+"='"+paraValue+"'");
					}else if("warehouseName".equals(paraName)|paraName.indexOf("warehouseName")!=-1){
						sql.append(" and "+paraName+" like '%"+paraValue+"%'");
					}
					
				}else if(paraName.indexOf("supplier")!=-1){//供应商表查询
					
					if("supplierNo".equals(paraName)|paraName.indexOf("supplierNo")!=-1){
						sql.append(" and "+paraName+"='"+paraValue+"'");
					}else if("supplierName".equals(paraName)|paraName.indexOf("supplierName")!=-1){
						sql.append(" and "+paraName+" like '%"+paraValue+"%'");
					}
					
				}else if(paraName.indexOf("product")!=-1){//商品表查询
					
					if("productNo".equals(paraName)|paraName.indexOf("productNo")!=-1){
						sql.append(" and "+paraName+"='"+paraValue+"'");
					}else if("productName".equals(paraName)|paraName.indexOf("productName")!=-1){
						sql.append(" and "+paraName+" like '%"+paraValue+"%'");
					}
					
				}else if((paraName.indexOf("max")!=-1|paraName.indexOf("Max")!=-1)|(paraName.indexOf("min")!=-1|paraName.indexOf("Min")!=-1)){
					
					if(paraName.indexOf("createAt")!=-1){
						if(paraName.indexOf("max")!=-1||paraName.indexOf("Max")!=-1)paraValue +=" 23:59:59";
						sql.append(" and "+paraName.substring(0,(paraName.length()>11?12:8))+((paraName.indexOf("max")!=-1||paraName.indexOf("Max")!=-1)?" <= '":" >= '")+paraValue+"'");
					}else if(paraName.indexOf("purDate")!=-1){
						if(paraName.indexOf("max")!=-1||paraName.indexOf("Max")!=-1)paraValue +=" 23:59:59";
						sql.append(" and "+paraName.substring(0,(paraName.length()>10?11:7))+((paraName.indexOf("max")!=-1||paraName.indexOf("Max")!=-1)?" <= '":" >= '")+paraValue+"'");
					}else if(paraName.indexOf("outDate")!=-1){
						if(paraName.indexOf("max")!=-1||paraName.indexOf("Max")!=-1)paraValue +=" 23:59:59";
						sql.append(" and "+paraName.substring(0,(paraName.length()>10?11:7))+((paraName.indexOf("max")!=-1||paraName.indexOf("Max")!=-1)?" <= '":" >= '")+paraValue+"'");
					}else if(paraName.indexOf("createdAt")!=-1){
						if(paraName.indexOf("max")!=-1||paraName.indexOf("Max")!=-1)paraValue +=" 23:59:59";
						sql.append(" and "+paraName.substring(0,(paraName.length()>10?13:9))+((paraName.indexOf("max")!=-1||paraName.indexOf("Max")!=-1)?" <= '":" >= '")+paraValue+"'");
					}
					
//					Min
				}else if(paraName.indexOf("status")!=-1){
					if(paraValue.indexOf(",")!=-1){
						String[] pa=paraValue.split(",");
						if(!pa[0].equals("-10"))sql.append(" and "+paraName+" between "+pa[0]+" and "+pa[1]);
					}else{
						sql.append(" and "+paraName+" = '"+paraValue+"'");
					}
				}else if(paraName.indexOf("permissionsNo")!=-1){
					sql.append(" and ("+paraName+" = '"+paraValue+"' or superior = '"+paraValue+"')");
				}
				/*else if((paraName.indexOf("max")!=-1|paraName.indexOf("Max")!=-1)|(paraName.indexOf("min")!=-1|paraName.indexOf("Min")!=-1)){
					
					if(paraName.indexOf("tuition")!=-1){//预计费用======>未确定
						
						sql.append(" and "+paraName.substring(0,7)+((paraName.indexOf("Max")!=-1||paraName.indexOf("max")!=-1)?" <= ":" >= ")+paraValue);
					
					}else if(paraName.indexOf("createTime")!=-1){
						if(paraName.indexOf("max")!=-1||paraName.indexOf("Max")!=-1)paraValue +=" 23:59:59";
						sql.append(" and "+paraName.substring(0,10)+((paraName.indexOf("max")!=-1||paraName.indexOf("Max")!=-1)?" <= '":" >= '")+paraValue+"'");
				
					}else if(paraName.indexOf("grade")!=-1){
						
						if(!paraValue.equals("-1"))sql.append(" and "+paraName.substring(0,5)+"No"+((paraName.indexOf("Max")!=-1||paraName.indexOf("max")!=-1)?" <= ":" >= ")+paraValue);
					
					}else if(paraName.indexOf("age")!=-1){
						
						sql.append(" and "+paraName.substring(0,3)+((paraName.indexOf("Max")!=-1||paraName.indexOf("max")!=-1)?" <= ":" >= ")+paraValue);
					
					} 
					
				}else if(paraName.indexOf("requireGrade")!=-1){//requireGrade年级
					String [] str=paraValue.split(",");
					if(str.length>0){
						record.set("requireGrade", paraValue);
						if(!str[0].equals("-1"))sql.append(" and requireGradeMinNo <= "+str[0]+" and "+str[0]+" <= requireGradeMaxNo ");
					}
					
				}else if(paraName.indexOf("startOrEnt")!=-1){//项目的时间查询拼接,项目特用的
					if(paraValue.length()>3){
						String [] str = paraValue.split(",");
						String start="",ent="";
						if(!str[0].equals("s")){
							record.set("startTime", str[0]);
							start = "and (to_days(startDatetime)-to_days('2017-01-01')) >= (to_days('"+str[0]+"')-to_days('"+str[0].substring(0,4)+"-01-01')) ";
						}
						if(!str[1].equals("e")){
							record.set("entTime", str[1]);
							ent = "and (to_days(endDatetime)-to_days('2017-01-01')) <= (to_days('"+str[1]+"')-to_days('"+str[0].substring(0,4)+"-01-01')) ";
						}
						if(!str[0].equals("s")||!str[1].equals("e")){
							sql.append(" and projectNo in (select  distinct projectNo from dl_prjtime where 1=1 "+start+ent+" )");
						}
					}
					
				}else if(paraName.indexOf("Time")!=-1|paraName.indexOf("time")!=-1){//精确匹配(如果含有Time却没有max表示是查询某个时间的数据)
					
					sql.append(" and "+paraName+((paraName.indexOf("startTime")!=-1||paraName.indexOf("starttime")!=-1)?" <= '":" >= '")+paraValue);
					
//					sql.append(" and "+paraName+"='"+paraValue+"'");
					
				}else if(paraName.indexOf("residential")!=-1){//精确匹配
					
					sql.append(" and "+paraName+"='"+paraValue+"'");
					
				}else if(paraName.indexOf("id")!=-1|paraName.indexOf("Id")!=-1){//精确匹配
					
					record.set(paraName, paraValue);//有时候id还有别的用处
					
					sql.append(" and "+paraName+"='"+paraValue+"'");
					
				}else if(paraName.indexOf("phone")!=-1|paraName.indexOf("Phone")!=-1){//精确匹配
					
					sql.append(" and "+paraName+"='"+paraValue+"'");
					
				}else if(paraName.indexOf("typeCity")!=-1){//模糊匹配，类型和城市
					record.set(paraName, paraValue);
					//sql.append(" and (type like '%"+paraValue+"%' or city like '%"+paraValue+"%') ");
					
				}else if(paraName.indexOf("sySType")!=-1|paraName.indexOf("syStype")!=-1){//精确匹配
					
					sql.append(" and type ='"+paraValue+"'");
					
				}else if(paraName.indexOf("state")!=-1|paraName.indexOf("State")!=-1){//精确匹配类别
					
					sql.append(" and "+paraName+"='"+paraValue+"'");
					
				}else if(paraName.indexOf("projectNo")!=-1|paraName.indexOf("ProjectNo")!=-1){//精确匹配类别
					
					sql.append(" and "+paraName+"='"+paraValue+"'");
					
				}*/else {
					record.set(paraName, paraValue);
				}
			}
			record.set(paraName, paraValue);
		}
		record.set("sql", sql.toString());
		inv.getController().setAttr("record", record);
		inv.invoke();
	}

}
